﻿using LD.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolFindShapeFrm : FrmToolBase
    {
        public ToolFindShapeFrm()
        {
            InitializeComponent();

            this.Text = TB_Tool.Name;
            this.btn_ok.Click += Btn_ok_Click;
            this.Shown += ToolReadImageFrm_Shown;
        }

        private void ToolReadImageFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var shape = new CreateShapeParam()
            {
                Camera = this.cob_camera.Text,
                Name = this.cob_shape.Text,
                FindModelParam = new FindShapeParam()
                {
                    StartAngle = (double)this.num_angleStart.Value,
                    EndAngle = (double)this.num_angleEnd.Value,
                    Greediness = (double)this.num_Greediness.Value,
                    MaxOverlap = (double)this.num_maxOverlap.Value,
                    MinScore = (double)this.num_minScore.Value,
                    NumLeavel = (int)this.num_leavel.Value,
                    NumMatches = (int)this.num_matches.Value,
                },
            };
            var param = Newtonsoft.Json.JsonConvert.SerializeObject(shape);
            TB_Tool.Param = param;
        }

        private void Init()
        {
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                this.cob_camera.Items.Add(item.Key);
            }

            if (TB_Tool.Param == null)
                return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<CreateShapeParam>(TB_Tool.Param);
            if (param != null)
            {
                this.cob_camera.Text = param.Camera;
                this.cob_shape.Text = param.Name;
                if (param.FindModelParam != null)
                {
                    this.num_angleStart.Value = (decimal)param.FindModelParam.StartAngle;
                    this.num_angleEnd.Value = (decimal)param.FindModelParam.EndAngle;
                    this.num_minScore.Value = (decimal)param.FindModelParam.MinScore;
                    this.num_matches.Value = param.FindModelParam.NumMatches;
                    this.num_maxOverlap.Value = (decimal)param.FindModelParam.MaxOverlap;
                    this.num_leavel.Value = param.FindModelParam.NumLeavel;
                    this.num_Greediness.Value = (decimal)param.FindModelParam.Greediness;
                }
            }
        }

        private void cob_camera_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cob_camera.SelectedIndex > -1)
            {
                LoadModel(cob_camera.Text);
            }
        }
        private void LoadModel(string camera)
        {
            this.cob_shape.Items.Clear();
            foreach (var model in CameraFactory.Instance.CameraList[camera].ShapeModels)
            {
                this.cob_shape.Items.Add(model.Key);
            }
        }

        private void cob_roi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cob_shape.SelectedIndex > -1)
            {

            }
        }
    }
}
